package com.cxh.mchat.bussiness.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxh.mchat.bussiness.bean.ConversationReqBean;
import com.cxh.mchat.bussiness.bean.ConversationRespBean;
import com.cxh.mchat.bussiness.bean.MessageCountBean;
import com.cxh.mchat.bussiness.entity.Conversation;
import com.cxh.mchat.bussiness.mapper.ConversationMapper;
import com.cxh.mchat.bussiness.service.IConversationService;
import com.cxh.mchat.bussiness.service.IMessageService;
import com.cxh.mchat.common.constant.StatusConstant;
import com.cxh.mchat.common.security.bean.LoginUser;
import com.cxh.mchat.common.utils.SecurityUtils;
import com.cxh.mchat.user.bean.UserContactInfo;
import com.cxh.mchat.user.entity.User;
import com.cxh.mchat.user.service.IUserContactsService;
import com.cxh.mchat.user.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 对话 服务实现类
 * </p>
 *
 * @author cxh
 * @since 2021-12-28
 */
@Service
public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Conversation> implements IConversationService {

    @Resource
    private IUserContactsService userContactsService;
    @Resource
    private IUserService userService;
    @Resource
    private IMessageService messageService;

    @Override
    public List<ConversationRespBean> queryRespList(ConversationReqBean reqBean) {
        //TODO 应该查缓存
        String bussinessName = reqBean.getBussinessName();
        if(StringUtils.isEmpty(bussinessName)){
            bussinessName = "";
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<Conversation> conversationList = getBaseMapper().selectList(new QueryWrapper<Conversation>()
                .eq("user_no", loginUser.getUserNo())
                .like("bussiness_name", bussinessName)
                .orderByDesc("last_update_time"));

        List<ConversationRespBean> resp = BeanUtil.copyToList(conversationList, ConversationRespBean.class);
        resp.forEach(item -> {
            UserContactInfo userContactInfo = userContactsService.queryContactsInfo(item.getBussinessNo());
            if(null != userContactInfo && null != userContactInfo.getBussinssUser()){
                item.setAvatar(userContactInfo.getBussinssUser().getAvatar());
                item.setUserContacts(userContactInfo);

                MessageCountBean messageCountBean = messageService.queryNoReadCount(loginUser.getUserNo(), userContactInfo.getBussinessNo());
                item.setNewMsgCount(messageCountBean.getNewMsgCount());
                item.setNewMsgPreview(messageCountBean.getNewMsgPreview());
            }
        });
        return resp;
    }

    @Override
    public Boolean add(Conversation reqBean) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Assert.notBlank(reqBean.getBussinessType(), "类型不能为空");
        Assert.notBlank(reqBean.getBussinessNo(), "用户账号/群聊ID不能为空");

        User contacts = userService.selectByUserNo(reqBean.getBussinessNo());

        reqBean.setStatus(StatusConstant.TRUE);
        reqBean.setUserNo(loginUser.getUserNo());
        reqBean.setBussinessName(contacts.getNickName());
        Conversation conversation = getBaseMapper().selectOne(new QueryWrapper<Conversation>()
                .eq("user_no", loginUser.getUserNo())
                .eq("bussiness_type", reqBean.getBussinessType())
                .eq("bussiness_no", reqBean.getBussinessNo()));
        if(null != conversation){
            updateById(conversation);
        }else{
            save(reqBean);
        }

        // 双方互相为对话关系
        Conversation conversationUser = new Conversation();
        conversationUser.setBussinessType(reqBean.getBussinessType());
        conversationUser.setStatus(StatusConstant.TRUE);
        conversationUser.setUserNo(reqBean.getBussinessNo());
        conversationUser.setBussinessNo(loginUser.getUserNo());
        conversationUser.setBussinessName(loginUser.getUser().getNickName());
        Conversation conversationdbUser = getBaseMapper().selectOne(new QueryWrapper<Conversation>()
                .eq("user_no", reqBean.getBussinessNo())
                .eq("bussiness_type", reqBean.getBussinessType())
                .eq("bussiness_no", loginUser.getUserNo()));
        if(null != conversationdbUser){
            return updateById(conversationdbUser);
        }
        return save(conversationUser);
    }
}
